﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Business_MessageReply : WebUI.Page.SendMessagePage
{
    public string address = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            address = GetAddress();
            ViewState["addr"] = address;
            if (address != "")
            {
                Lbl_Address.Text = "<span style='font-weight:bold;'>" + address + "</span>";
            }

            BindHour();
        }
    }

    #region 获得规则标识 int GetSMSID()
    /// <summary>
    /// 获得规则标识
    /// </summary>
    private string GetAddress()
    {
        string strAdd = "";
        if (Request.QueryString["Address"] != null)
        {
            strAdd = Request.QueryString["Address"].ToString();
        }

        return strAdd;
    }
    #endregion


    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        //获取定时发送的时间
        DateTime sendTime = Convert.ToDateTime(tb_SendTime.Text + " " +
            ddl_SendHour.SelectedValue + ":" + ddl_SendMinite.SelectedValue + ":00 ");

        if (!CompareTowDateTimes(DateTime.Now, sendTime))
        {
            lbl_CheckSendTime.Text = "<span style='color:red; font-weight:bold;'>定时发送时间必须大于当前时间！</span>";
        }

        if (IsMobile(ViewState["addr"].ToString()))
        {
            #region 立即发送
            //立即发送
            if (rb_Now.Checked == true)
            {
                SendSms(ViewState["addr"].ToString(), tb_MsgContent.Text, new DateTime());
            }
            #endregion
            #region 定时发送
            //定时发送
            else
            {
                //发送短信
                SendSms(ViewState["addr"].ToString(), tb_MsgContent.Text, sendTime);
            }
        }
        #endregion
    }

    #region 发送短信操作
    /// <summary>
    /// 发送短信操作
    /// </summary>
    /// <param name="Address">发送号码</param>
    /// <param name="Content">短信内容</param>
    /// <param name="SendTime">发送时间</param>
    private void SendSms(string Address, string Content, DateTime SendTime)
    {
        int result = 0;

        if (DateTime.Now > SendTime)
        {
            result = SendMessageWithRecord(Address, Content);
        }
        else
        {
            result = SendMessageWithRecord(Address, Content, SendTime);
        }

        if (result > 0)
        {
            InsertShortMessage(Address, Content);
            PageRight("短信发送成功!", "Orders.aspx", "返回操作", "");
            
        }
        else
        {
            PageError("发送短信失败!", "Orders.aspx");
        }
    }
    #endregion

    #region 绑定小时时间
    /// <summary>
    /// 绑定小时时间
    /// </summary>
    private void BindHour()
    {
        for (int i = 0; i < 24; i++)
        {
            ListItem li = new ListItem();
            li.Value = i.ToString();
            li.Text = i.ToString();

            ddl_SendHour.Items.Add(li);
        }

        for (int j = 0; j < 60; j++)
        {
            ListItem item = new ListItem();
            item.Text = j.ToString();
            item.Text = j.ToString();

            ddl_SendMinite.Items.Add(item);
        }
    }
    #endregion

    #region 选择即时或定时发送短信
    /// <summary>
    /// 选中定时发送
    /// </summary>
    protected void rb_Timing_CheckedChanged(object sender, EventArgs e)
    {
        if (rb_Timing.Checked)
        {
            div_Timing.Visible = true;
        }
    }
    /// <summary>
    /// 选中立即发送
    /// </summary>
    protected void rb_Now_CheckedChanged(object sender, EventArgs e)
    {
        if (rb_Now.Checked)
        {
            div_Timing.Visible = false;
        }
    }
    #endregion

    #region 返回事件
    protected void Btn_Back_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Orders.aspx");
    }
    #endregion
}
